package com.deluxelx.soul.inn.service.file.impl;

import cn.hutool.core.util.StrUtil;
import com.deluxelx.soul.inn.bean.course.Course;
import com.deluxelx.soul.inn.service.course.CourseService;
import com.deluxelx.soul.inn.service.file.FileStrategyConfig;
import com.deluxelx.soul.inn.service.file.IFileStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import static com.deluxelx.soul.inn.common.constant.FileType.COURSE_IMG;

/**
 * 课程配图
 *
 * @author liaozihao
 * @date 2024-04-30 10:31
 */
@Component("courseImgStrategy")
@Slf4j
public class CourseImgStrategy extends FileStrategyConfig implements IFileStrategy {

    @Resource
    private CourseService courseService;

    @Override
    public Boolean upload(MultipartFile file, String primaryId) {
        try {
            String url = ossFileUtil.upload(file, COURSE_IMG);
            if (StrUtil.isBlank(url)) {
                return false;
            }
            return courseService.lambdaUpdate()
                    .set(Course::getBacImg, url)
                    .eq(Course::getId, primaryId)
                    .update();
        } catch (Exception e) {
            log.error("上传课程配图失败", e);
            return false;
        }
    }

    @PostConstruct
    public void init() {
        fileStrategyMap.put(COURSE_IMG, this);
    }
}
